package com.foreveross.project.usercenterserver.common.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author: yz
 * @Date: 2019/7/22 11:16 AM
 */
public class ContextUtil {


    public static HttpServletRequest getRequest() {

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (servletRequestAttributes == null) {
            return null;
        }
        return servletRequestAttributes.getRequest();
    }

    static HttpSession getSession() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getSession();
    }

    static Object getFromSession(String key) {
        HttpSession session = getSession();
        if (session == null) {
            return null;
        }
        return session.getAttribute(key);
    }


    public static String getCurrentUserId() {
        return (String) getFromSession("userId");
    }

}
